import os
from util import tuple_generator
from util import model


def create_triplets(tx, triplets):
    # 创建节点和关系
    for triplet in triplets:
        subject, predicate, object_ = triplet

        # 创建主体节点
        tx.run(
            "MERGE (s:Entity {name: $subject})",
            subject=subject

        )

        # 创建客体节点
        tx.run(
            "MERGE (o:Entity {name: $object})",
            object=object_
        )

        # 创建关系
        tx.run(
            """
            MATCH (s:Entity {name: $subject})
            MATCH (o:Entity {name: $object})
            MERGE (s)-[:HAS_PROPERTY {predicate: $predicate}]->(o)
            """,
            subject=subject, predicate=predicate, object=object_
        )


def to_sink():
    # 检查目录是否存在
    if not os.path.exists('data'):
        print(f"目录 '{'data'}' 不存在")
        return
    # 遍历目录中的文件
    for filename in os.listdir('data'):
        # 检查路径是否为文件
        if os.path.isfile(os.path.join('data', filename)):
            filename = os.path.join('data', filename)
            with open(filename, 'r', encoding='utf-8') as file:
                for line in file:
                    element_list = model.getTuple(line)
                    triplets = []
                    for element in element_list:
                        element = element.strip('()').split(', ')
                        triplet = tuple(element)
                        if len(triplet) == 3:
                            triplets.append(triplet)
                    print(filename)
                    print(triplets)

                    # with pool.driver.session() as session:
                    #     session.execute_write(create_triplets, triplets)


if __name__ == '__main__':
    to_sink()
